<template>
	<view>
		<uni-nav-bar :fixed="true" left-icon="back" title="设置支付密码" @clickLeft="leftBack" background-color="#318cff" color="#fff"></uni-nav-bar>
		<view class="dfj-bg-white dfj-pd-lr-30">
			<view class="dfj-pd-tb-30 dfj-t-333 dfj-bd-b-e5e">{{ loginInfo.userInfo.mobilePhone }}</view>
			<view class="dfj-flex dfj-flex-ai-c dfj-bd-b-e5e">
				<view class="dfj-flex-1"><input class="dfj-pd-tb-30" v-model="form.vaildCode" placeholder="请输入验证码" style="width: 100%;" type="number"/></view>
				<view class="dfj-t-base" @click="getCode()">{{ codeTxt }}</view>
			</view>
			<view class="dfj-bd-b-e5e">
				<input class="dfj-pd-tb-30" style="width: 100%;" v-model="form.Pwd" type="password" placeholder="请输入密码(密码为6位,由数字组成)"/>
			</view>
			<view>
				<input class="dfj-pd-tb-30" style="width: 100%;" v-model="form.newPwd" type="password" placeholder="确认密码(密码为6位,由数字组成)"/>
			</view>
			<view class="dfj-pd-tb-50">
				<button type="primary" style="width: 100%;border-radius: 20rpx;" @click="setPwd()">确认</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isCode: true,
				codeTxt: '获取验证码',
				codeTime: 60,
				loginInfo: '',
				form: {
					Pwd: '',
					newPwd: '',
					vaildCode: ''
				}
			}
		},
		onShow() {
			this.loginInfo = JSON.parse(localStorage.getItem('loginInfo'));
		},
		methods: {
			leftBack() {
				uni.navigateBack();
			},
			getCode() {
				if(this.isCode){
					this.$http({
						url: '/userOpt/setPayPwdVailCode'
					}).then(res=> {
						if(res.flag){
							uni.showToast({
								title: '验证码已发送'
							})
							this.isCode = false;
							let stime = setInterval(()=>{
								this.codeTime--;
								this.codeTxt = this.codeTime+'s';
								if(this.codeTime <= 0){
									clearInterval(stime)
									this.isCode = true;
									this.codeTime = 60;
									this.codeTxt = '重新获取';
								}
							},1000)
						}
					})
				}
			},
			setPwd() {
				if(this.form.newPwd.length != 6){
					uni.showToast({
						title: '支付密码长度只能为6位',
						icon: 'none'
					})
					setTimeout(()=>{
						uni.hideToast();
					},1500)
					return false
				}
				if(this.form.Pwd != this.form.newPwd){
					uni.showToast({
						title: '两次输入不一致',
						icon: 'none'
					})
					setTimeout(()=>{
						uni.hideToast();
					},1500)
					return false
				}
				this.$http({
					url: '/userOpt/setPayPwd',
					data: this.form
				}).then(res=> {
					console.log(res)
					if(res.flag){
						uni.showToast({
							title: '设置成功'
						})
						this.loginInfo.userInfo.setPayPwd = 1
						localStorage.setItem('loginInfo',JSON.stringify(this.loginInfo))
						setTimeout(()=>{
							uni.hideToast();
							uni.navigateBack()
						},1500)
					}
				})
			}
		}
	}
</script>

<style>
</style>
